<template>
  <van-nav-bar left-arrow title="订单结算" fixed @click-left="onBack" />

  <div style="height: 46px"></div>

  <van-contact-card
    type="add"
    add-text="选择收货人"
    @click="onAdd"
    v-if="JSON.stringify(AddressInfo) === '{}'"
  />

  <van-contact-card
    type="edit"
    :tel="AddressInfo.mobile"
    :name="AddressInfo.consignee"
    v-else
    @click="onEdit"
  />

  <!-- 选择的购物车的商品数据 -->
  <van-card
    v-for="item in CartList"
    :key="item.id"
    :num="item.pronum"
    :price="item.total"
    :title="'product' in item ? item.product.name : item.name"
    :thumb="'product' in item ? item.product.cover_cdn : item.cover_cdn"
  >
    <template #desc>
      <!--  -->
      <span>商品单价：￥{{ item.price }}</span>
    </template>
  </van-card>

  <!-- 订单备注 -->
  <van-cell-group inset>
    <van-field
      v-model="content"
      rows="2"
      autosize
      label="订单备注"
      type="textarea"
      maxlength="50"
      placeholder="请输入订单备注"
      show-word-limit
    />
  </van-cell-group>

  <van-submit-bar :price="total" button-text="结算" @submit="onSubmit" />
</template>

<script>
import { showNotify } from "vant";
export default {
  data() {
    return {
      AddressInfo: {},
      LoginAuth: {},
      CartList: [],
      content: "",
    };
  },
  created() {
    let address = this.$cookies.get("address")
      ? this.$cookies.get("address")
      : {};

    this.LoginAuth = this.$cookies.get("LoginAuth")
      ? this.$cookies.get("LoginAuth")
      : {};

    if (JSON.stringify(address) !== "{}") {
      this.AddressInfo = address;
    } else {
      this.getAddressDefault();
    }

    this.getConfirmData();
  },
  methods: {
    onBack() {
      this.$router.go(-1);
    },
    onAdd() {},
    async getAddressDefault() {
      let data = {
        busid: this.LoginAuth.id,
      };

      let result = await this.$api.AddressDefault(data);

      if (result.code === 1) {
        this.AddressInfo = result.data;
      }
    },
    onEdit() {
      this.$router.push({
        path: "/business/address/index",
        query: { action: "order" },
      });
    },
    async getConfirmData() {
      // CartConfirmData

      // 如果从购物车页面进入，就获取到购物车列表id
      let cartids = this.$route.query.cartids ? this.$route.query.cartids : "";

      // 如果从商品详情页面进入，就获取到商品id
      let proid = this.$route.query.proid ? this.$route.query.proid : "";
      // console.log(proid);

      let result = await this.$api.CartConfirmData({ cartids, proid });
      // console.log(result);

      if (result.code === 0) {
        showNotify({
          type: "warning",
          message: result.msg,
          duration: 1500,
          onClose: () => {
            this.$router.go(-1);
          },
        });

        return false;
      }

      this.CartList = result.data;
    },
    async onSubmit() {
      // 如果从购物车页面进入，就获取到传入的购物车列表id
      let cartids = this.$route.query.cartids ? this.$route.query.cartids : "";

      // 如果从商品详情页面进入，就获取到传入的商品id和价格
      let proid = this.$route.query.proid ? this.$route.query.proid : "";
      let total = this.$route.query.total ? this.$route.query.total : "";

      let data = {
        cartids: cartids,
        busid: this.LoginAuth.id,
        content: this.content,
        addrid: this.AddressInfo.id,
      };

      let result = null;

      // 如果商品id存在，就走立即购买的逻辑
      if (proid && total) {
        data.cartids = 1;
        data.proid = proid;
        data.total = total;
        // console.log(data);
        result = await this.$api.ProductBuyNow(data);
      } else {
        // console.log(data);
        result = await this.$api.OrderSubmit(data);
      }

      if (result.code === 1) {
        showNotify({
          type: "success",
          message: result.msg,
          duration: 1500,
          onClose: () => {
            this.$cookies.remove("address");

            this.$router.push("/order/order/index");
          },
        });
      } else {
        showNotify({
          type: "warning",
          message: result.msg,
          duration: 1500,
        });
      }
    },
  },
  computed: {
    total() {
      let total = 0;

      // 如果获取到商品价格，就说明是从商品详情页点击立即购买进来的
      if (this.$route.query.total) {
        total = this.$route.query.total;

        return total * 100;
      }

      // 从购物车进入
      if (this.CartList.length > 0) {
        for (let item of this.CartList) {
          total += parseFloat(item.total);
        }
      }

      return total * 100;
    },
  },
};
</script>